package gui;

import java.awt.Component;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import user.User;

import controller.MessageController;

import network.SendSignalException;
import file.BadFileTypeException;

public class SendFileAction extends AbstractAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Component parent ;
	private MessageController msgCtrl ;
	private User userTo ;
	
	public SendFileAction (Component parent, MessageController msgCtrl, User to) {
		super () ;
		this.parent = parent ;
		this.msgCtrl = msgCtrl ;
		this.userTo = to ;
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		JFileChooser jfc = new JFileChooser() ;
		if (jfc.showOpenDialog(this.parent) == JFileChooser.APPROVE_OPTION) {
			try {
				this.msgCtrl.sendFile(jfc.getSelectedFile(), this.userTo) ;
			} 
			catch (BadFileTypeException e) {
				JOptionPane.showMessageDialog(this.parent, "Impossible d'ouvrir le fichier spécifié.", "Erreur de lecture", JOptionPane.ERROR_MESSAGE) ;
			} 
			catch (SendSignalException e) {
				JOptionPane.showMessageDialog(this.parent, "Erreur durant l'envoi du fichier.", "Erreur d'envoi", JOptionPane.ERROR_MESSAGE) ;
				e.printStackTrace();
			}
		}
	}

}
